﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KZ.Express.Setup.KZSystem
{
    public partial class LoginAccountDetailView : FormBase
    {
        private void LoginAccountDetailView_Load(object sender, EventArgs e)
        {
            LoadRole();
            LoadData();
        }

        protected override void LoadData()
        {
            try
            {
                switch (base.Action)
                {
                    case ActionName.Insert:
                        break;

                    case ActionName.Update:
                        _presenter.LoadEntity();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadRole()
        {
            try
            {
                Global.LoadComboBox(cmbRoleID, _presenter.LoadForCMB<RoleENT>(null));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        public LoginAccountDetailView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.LoginAccount;
            base._presenter = new PresenterBase(this, new UserENT());
        }

        #region Public Properties

        public override EntityBase Entity
        {
            get
            {
                UserENT user = new UserENT();
                user.UserID = base.EntityID;
                user.FullName = txtFullName.Text;
                user.Username = txtUsername.Text;
                user.Password = txtPassword.Text;
                user.RoleID = Utility.IntNParse(cmbRoleID.SelectedValue);
                user.CreatedBy = UserENT.USERID;
                user.UpdatedBy = UserENT.USERID;

                return user;
            }
            set
            {
                if (value == null)
                {
                    ClearControl();
                    LoadData();
                }
                else
                {
                    UserENT user = value as UserENT;
                    txtFullName.Text = user.FullName;
                    txtUsername.Text = user.Username;
                    txtPassword.Text = user.Password;
                    if (user.RoleID.HasValue)
                        cmbRoleID.SelectedValue = user.RoleID;
                    else
                        cmbRoleID.SelectedValue = DBNull.Value;
                }
            }
        }

        #endregion Public Properties
    }
}
